/*
 *所有关于Bas_Company类的业务代码应在此处编写
 *可使用repository.调用常用方法，获取EF/Dapper等信息
 *如果需要事务请使用repository.DbContextBeginTransaction
 *也可使用DBServerProvider.手动获取数据库相关信息
 *用户信息、权限、角色等使用UserContext.Current操作
 *Bas_CompanyService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
 */

using DGZImp.Core.Utilities;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace DGZImp.BasProject.Services
{
    public partial class Bas_CompanyService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IBas_CompanyRepository _repository; //访问数据库
        private readonly WebResponseContent _webResponse = new();

        [ActivatorUtilitiesConstructor]
        public Bas_CompanyService(
            IBas_CompanyRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
        )
            : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            AddOnExecuting = (data, _) =>
            {
                if (_repository.FindFirst(a =>
                        a.CompanyCode == data.CompanyCode && a.IsValid == 1 && a.GroupId == data.GroupId) is
                    { } tempData)
                    return _webResponse.Error($"存在相同编号:\"{tempData.CompanyCode}\" 公司:\" {tempData.CompanyName}\"");
                return _webResponse.OK();
            };
            return base.Add(saveDataModel);
        }

        public override WebResponseContent Update(SaveModel saveDataModel)
        {
            UpdateOnExecuting = (data, _, _, _) =>
            {
                if (_repository.FindFirst(a =>
                        a.CompanyCode == data.CompanyCode && a.IsValid == 1 && a.CompanyId != data.CompanyId &&  a.GroupId == data.GroupId) is
                    { } tempData)
                    return _webResponse.Error($"存在相同编号:\"{tempData.CompanyCode}\" 公司:\" {tempData.CompanyName}\"");
                return _webResponse.OK();
            };
            return base.Update(saveDataModel);
        }
        
    }
}